<template>
	<el-menu class="side el-menu-vertical-demo" default-active="/" @open="handleOpen" @close="handleClose"
		background-color="#545c64" text-color="#fff" active-text-color="#38f" :collapse="isCollapse" :router="true">
		<el-menu-item index="/">
			<i class="el-icon-s-home"></i>
			<span slot="title">后台首页</span>
		</el-menu-item>

		<el-submenu index="web"
			v-show="user_group == '管理员' || $check_group(['/nav/table','/link/table','/slides/table','/ad/table'])">
			<template slot="title"><i class="el-icon-picture-outline"></i><span>轮播图</span></template>
			<!-- <el-menu-item v-show="user_group == '管理员' || $check_action('/auth/table')" index="/auth/table">
				<span>权限管理</span>
			</el-menu-item> -->
			<!-- <el-menu-item v-show="user_group == '管理员' || $check_action('/link/table')" index="/link/table">
				<span>友情链接</span>
			</el-menu-item> -->
			<el-menu-item v-show="user_group == '管理员' || $check_action('/slides/table')" index="/slides/table">
				<span>轮播图管理</span>
			</el-menu-item>
			<!-- <el-menu-item v-show="user_group == '管理员' || $check_action('/ad/table')" index="/ad/table">
				<span>广告</span>
			</el-menu-item> -->
		</el-submenu>

		<el-submenu index="notic"
			v-show="user_group == '管理员' || $check_group(['/notice/table', '/feedback/table'])">
			<template slot="title"><i class="el-icon-warning-outline"></i><span>公告消息管理</span></template>
			<el-menu-item v-show="user_group == '管理员' || $check_action('/notice/table')" index="/notice/table">
				<span>公告消息</span>
			</el-menu-item>
			<el-menu-item v-show="user_group == '管理员' || $check_action('/feedback/table')" index="/feedback/table">
				<span>用户反馈</span>
			</el-menu-item>
		</el-submenu>


	<el-submenu index="news"
		v-show="$check_group(['/article/table','/article_type/table','/exam/table'])">
		<template slot="title"><i class="el-icon-s-opportunity"></i><span>资源管理</span></template>

				<el-menu-item v-show="user_group == '管理员' || $check_action('/article/table')" index="/article/table">
			<!-- <span>文章</span> -->
			<span>{{$page_title("/article/table") || "文章"}}</span>
		</el-menu-item>
		<el-menu-item v-show="user_group == '管理员' || $check_action('/article_type/table')"
			index="/article_type/table">
			<!-- <span>文章类型</span> -->
			<span>{{$page_title("/article_type/table") || "文章分类"}}</span>
		</el-menu-item>
							</el-submenu>

	<el-submenu index="meeting"
		v-show="user_group == '管理员' || $check_group(['/meeting_information/table','/committee_member/table','/speaker/table','/call_for_papers/table'])">
		<template slot="title"><i class="el-icon-s-flag"></i><span>会议管理</span></template>

		<el-menu-item v-show="user_group == '管理员' || $check_action('/meeting_information/table')" index="/meeting_information/table">
			<span>会议信息</span>
		</el-menu-item>
		<el-menu-item v-show="user_group == '管理员' || $check_action('/committee_member/table')" index="/committee_member/table">
			<span>大会组委</span>
		</el-menu-item>
		<el-menu-item v-show="user_group == '管理员' || $check_action('/speaker/table')" index="/speaker/table">
			<span>报告嘉宾</span>
		</el-menu-item>
		<el-menu-item v-show="user_group == '管理员' || $check_action('/call_for_papers/table')" index="/call_for_papers/table">
			<span>征稿主题</span>
		</el-menu-item>
	</el-submenu>
	



		<el-submenu index="user"
					v-show="user_group == '管理员' || $check_group(['/user/table','/user_group/table'
						,'/ordinary_users/table'
																	])">
			<template slot="title"><i class="el-icon-s-custom"></i><span>系统用户</span></template>
			<el-menu-item index="/user/table" v-show="user_group == '管理员'"><span>管理员</span></el-menu-item>
							<el-menu-item index="/ordinary_users/table" v-show="user_group == '管理员' || $check_action('/ordinary_users/table')"><span>普通用户</span></el-menu-item>
																	<!-- <el-menu-item index="/user_group/table"><span>用户组</span></el-menu-item> -->
		</el-submenu>


		<!-- 专家管理模块 - 已注释，如需启用请取消注释 -->
		<!--
		<el-submenu index="expert" v-show="expertAuth.length > 0">
			<template slot="title"><i class="el-icon-user-solid"></i><span>专家管理</span></template>
			<el-menu-item v-for="(o, idx) in expertAuth" v-show="$check_action(o.path)" :index="o.path" :key="'expert-' + idx">
				<span>{{o.page_title}}</span>
			</el-menu-item>
		</el-submenu>
		-->

		<!-- 用户管理模块 -->
		<el-submenu index="user_manage" v-show="userAuth.length > 0">
			<template slot="title"><i class="el-icon-s-custom"></i><span>用户管理</span></template>
			<el-menu-item v-for="(o, idx) in userAuth" v-show="$check_action(o.path)" :index="o.path" :key="'user-' + idx">
				<span>{{o.page_title}}</span>
			</el-menu-item>
		</el-submenu>

		<!-- 其他模块（如果还有不属于专家或用户的模块） -->
		<el-submenu index="more" v-show="otherAuth.length > 0">
			<template slot="title"><i class="el-icon-menu"></i><span>其他模块</span></template>
			<el-menu-item v-for="(o, idx) in otherAuth" v-show="$check_action(o.path)" :index="o.path" :key="'other-' + idx">
				<span>{{o.page_title}}</span>
			</el-menu-item>
		</el-submenu>
	</el-menu>
</template>

<script>
	export default {
		props: {
			isCollapse: {
				trye: Boolean,
				default: false
			}
		},
		data: function() {
			return {
				user_group: this.$store.state.user.user_group
			}
		},
		methods: {
			handleOpen(key, keyPath) {
				// console.log(key, keyPath);
			},

			handleClose(key, keyPath) {
				// console.log(key, keyPath);
			},

		},
		computed: {
		// 原始auth列表（保留用于兼容）
		auth() {
			var tables = this.$store.state.web.auth;
			var lt = [];
			for (var i = 0; i < tables.length; i++) {
				var o = tables[i];
				if (o.path.indexOf("/table") !== -1) {
					let list = ["exam","article", "article_type", "comment","notice","slides"];
						list.push("expert_users");
							list.push("ordinary_users");
						// 会议管理相关模块
						list.push("meeting_information");
						list.push("committee_member");
						list.push("speaker");
						list.push("call_for_papers");
																			if (list.indexOf(o.table_name) === -1) {
						lt.push(o);
					}
				}
			}
			return lt;
		},
		// 专家相关模块 - 已注释，如需启用请取消注释
		expertAuth() {
			// 专家模块功能已注释，返回空数组
			return [];
			/*
			var tables = this.$store.state.web.auth;
			var lt = [];
			for (var i = 0; i < tables.length; i++) {
				var o = tables[i];
				if (o.path.indexOf("/table") !== -1) {
					// 排除已经在固定菜单中显示的模块
					let excludeList = ["exam","article", "article_type", "comment","notice","slides"];
					excludeList.push("expert_users");
					excludeList.push("ordinary_users");
					excludeList.push("meeting_information");
					excludeList.push("committee_member");
					excludeList.push("speaker");
					excludeList.push("call_for_papers");
					
					// 排除已经在固定菜单中的模块
					if (excludeList.indexOf(o.table_name) !== -1) {
						continue;
					}
					
					// 判断是否为专家相关模块（通过路径或表名判断）
					var path = o.path.toLowerCase();
					var tableName = (o.table_name || '').toLowerCase();
					var pageTitle = (o.page_title || '').toLowerCase();
					
					// 如果路径或表名包含 expert，则为专家模块
					if (path.indexOf('expert') !== -1 || tableName.indexOf('expert') !== -1 || pageTitle.indexOf('专家') !== -1) {
						lt.push(o);
					}
				}
			}
			return lt;
			*/
		},
		// 用户相关模块
		userAuth() {
			var tables = this.$store.state.web.auth;
			var lt = [];
			for (var i = 0; i < tables.length; i++) {
				var o = tables[i];
				if (o.path.indexOf("/table") !== -1) {
					// 排除已经在固定菜单中显示的模块
					let excludeList = ["exam","article", "article_type", "comment","notice","feedback","slides"];
					excludeList.push("expert_users");
					excludeList.push("ordinary_users");
					excludeList.push("user"); // 排除管理员（user表）
					excludeList.push("meeting_information");
					excludeList.push("committee_member");
					excludeList.push("speaker");
					excludeList.push("call_for_papers");
					
					// 排除已经在固定菜单中的模块
					if (excludeList.indexOf(o.table_name) !== -1) {
						continue;
					}
					
					// 排除管理员模块（/user/table）
					var path = o.path.toLowerCase();
					if (path === '/user/table' || path.indexOf('/user/table') !== -1) {
						continue; // 跳过管理员模块
					}
					
					// 排除专家模块（避免重复）
					var tableName = (o.table_name || '').toLowerCase();
					var pageTitle = (o.page_title || '').toLowerCase();
					
					if (path.indexOf('expert') !== -1 || tableName.indexOf('expert') !== -1 || pageTitle.indexOf('专家') !== -1) {
						continue; // 跳过专家模块
					}
					
					// 排除管理员相关（通过页面标题判断）
					if (pageTitle.indexOf('管理员') !== -1) {
						continue; // 跳过管理员
					}
					
					// 判断是否为用户相关模块（通过路径或表名判断）
					// 如果路径或表名包含 user，则为用户模块
					if (path.indexOf('user') !== -1 || tableName.indexOf('user') !== -1 || pageTitle.indexOf('用户') !== -1) {
						lt.push(o);
					}
				}
			}
			
			// 按照指定顺序排序
			var orderMap = {
				'user_participation': 1,  // 用户参会
				'user_accommodation': 2,   // 用户住宿
				'user_seat_arrangement': 3, // 用户排座
				'user_notifications': 4,   // 用户通知
				'user_check_in': 5,       // 用户签到
				'user_sign_out': 6        // 用户签退
			};
			
			// 定义标题映射（用于匹配中文标题）
			var titleMap = {
				'用户参会': 1,
				'用户住宿': 2,
				'用户排座': 3,
				'用户通知': 4,
				'用户签到': 5,
				'用户签退': 6
			};
			
			lt.sort(function(a, b) {
				var pathA = (a.path || '').toLowerCase();
				var pathB = (b.path || '').toLowerCase();
				var tableNameA = (a.table_name || '').toLowerCase();
				var tableNameB = (b.table_name || '').toLowerCase();
				var pageTitleA = a.page_title || '';
				var pageTitleB = b.page_title || '';
				
				// 获取排序优先级
				var orderA = 999;
				var orderB = 999;
				
				// 先尝试通过路径匹配
				for (var key in orderMap) {
					if (pathA.indexOf(key) !== -1 || tableNameA.indexOf(key) !== -1) {
						orderA = orderMap[key];
						break;
					}
				}
				
				for (var key in orderMap) {
					if (pathB.indexOf(key) !== -1 || tableNameB.indexOf(key) !== -1) {
						orderB = orderMap[key];
						break;
					}
				}
				
				// 如果路径匹配失败，尝试通过标题匹配
				if (orderA === 999) {
					for (var title in titleMap) {
						if (pageTitleA.indexOf(title) !== -1) {
							orderA = titleMap[title];
							break;
						}
					}
				}
				
				if (orderB === 999) {
					for (var title in titleMap) {
						if (pageTitleB.indexOf(title) !== -1) {
							orderB = titleMap[title];
							break;
						}
					}
				}
				
				return orderA - orderB;
			});
			
			return lt;
		},
		// 其他模块（既不是专家也不是用户）
		otherAuth() {
			var tables = this.$store.state.web.auth;
			var lt = [];
			for (var i = 0; i < tables.length; i++) {
				var o = tables[i];
				if (o.path.indexOf("/table") !== -1) {
					// 排除已经在固定菜单中显示的模块
					let excludeList = ["exam","article", "article_type", "comment","notice","feedback","slides"];
					excludeList.push("expert_users");
					excludeList.push("ordinary_users");
					excludeList.push("meeting_information");
					excludeList.push("committee_member");
					excludeList.push("speaker");
					excludeList.push("call_for_papers");
					excludeList.push("upload"); // 排除文件上传模块
					
					// 排除已经在固定菜单中的模块
					if (excludeList.indexOf(o.table_name) !== -1) {
						continue;
					}
					
					// 排除专家和用户模块
					var path = o.path.toLowerCase();
					var tableName = (o.table_name || '').toLowerCase();
					var pageTitle = (o.page_title || '').toLowerCase();
					
					var isExpert = path.indexOf('expert') !== -1 || tableName.indexOf('expert') !== -1 || pageTitle.indexOf('专家') !== -1;
					var isUser = path.indexOf('user') !== -1 || tableName.indexOf('user') !== -1 || pageTitle.indexOf('用户') !== -1;
					
					// 排除文件上传模块
					if (path.indexOf('upload') !== -1 || tableName.indexOf('upload') !== -1 || pageTitle.indexOf('文件上传') !== -1) {
						continue;
					}
					
					// 既不是专家也不是用户模块的，放入其他模块
					if (!isExpert && !isUser) {
						lt.push(o);
					}
				}
			}
			return lt;
		}
		}
	}
</script>


<style scoped="scoped">
	.el-menu-vertical-demo:not(.el-menu--collapse) {
		width: 16rem;
	}

	.side {
		float: left;
		background: #545c64;
		color: #fff;
		overflow-y: scroll;
		height: 100vh;
	}
	.side::-webkit-scrollbar{
		display: none;
	}
</style>
